{{!
  Copyright (c) HashiCorp, Inc.
  SPDX-License-Identifier: BUSL-1.1
~}}

<form {{action (queue (action "preSave" this.model) (perform this.save this.model)) on="submit"}}>
  <MessageError @model={{this.model}} data-test-edit-form-error />
  <div class="box is-sideless is-fullwidth is-marginless">
    <NamespaceReminder @mode="save" />
    {{#if (eq @mode "create")}}
      <FormField data-test-field @attr={{hash name="name" type="string"}} @model={{this.model}} />
    {{/if}}
    <div class="control is-flex box is-shadowless is-fullwidth is-marginless">
      <input
        data-test-input="operationNone"
        id="operationNone"
        type="checkbox"
        class="toggle is-success is-small"
        checked={{not this.model.operationNone}}
        onchange={{action "toggleOperationSpecial" value="target.checked"}}
      />
      <label for="operationNone" class="has-text-weight-bold is-size-8">
        Allow this role to perform KMIP operations
      </label>
    </div>
    {{#unless this.model.operationNone}}
      <Toolbar>
        <h3 class="title is-6 has-left-padding-s">
          Allowed Operations
        </h3>
      </Toolbar>
      <div class="box">
        <FormField
          @attr={{hash name="operationAll" type="boolean" options=(hash label="Allow this role to perform all operations")}}
          @model={{this.model}}
        />
        <hr />
        <div class="is-flex">
          <div class="kmip-role-allowed-operations">
            {{#each-in (get this.model.operationFormFields 0) as |groupName fieldsInGroup|}}
              <h4 class="title is-7">{{groupName}}</h4>
              {{#each fieldsInGroup as |attr|}}
                <FormField
                  data-test-field
                  @disabled={{or this.model.operationNone this.model.operationAll}}
                  @attr={{attr}}
                  @model={{compute (action "placeholderOrModel") this.model.operationAll attr}}
                  @showHelpText={{false}}
                />
              {{/each}}
            {{/each-in}}
          </div>
          <div class="kmip-role-allowed-operations">
            {{#each (drop 1 (or this.model.operationFormFields (array))) as |group|}}
              <div class="kmip-role-allowed-operations">
                {{#each-in group as |groupName fieldsInGroup|}}
                  <h4 class="title is-7">{{groupName}}</h4>
                  {{#each fieldsInGroup as |attr|}}
                    <FormField
                      data-test-field
                      @disabled={{or this.model.operationNone this.model.operationAll}}
                      @attr={{attr}}
                      @model={{compute (action "placeholderOrModel") this.model.operationAll attr}}
                      @showHelpText={{false}}
                    />
                  {{/each}}
                {{/each-in}}
              </div>
            {{/each}}
          </div>
        </div>
      </div>
    {{/unless}}
    <div class="box is-fullwidth is-shadowless">
      <h3 class="title is-3">
        TLS
      </h3>
      {{#each this.model.tlsFormFields as |attr|}}
        <FormField data-test-field @attr={{attr}} @model={{this.model}} />
      {{/each}}
    </div>
    {{#each this.model.fields as |attr|}}
      <FormField data-test-field @attr={{attr}} @model={{this.model}} />
    {{/each}}
  </div>

  <div class="field is-grouped is-grouped-split is-fullwidth box is-bottomless">
    <Hds::ButtonSet>
      <Hds::Button
        @text={{this.saveButtonText}}
        @icon={{if this.save.isRunning "loading"}}
        type="submit"
        data-test-edit-form-submit
        disabled={{this.save.isRunning}}
      />
      {{#if this.cancelLink}}
        <Hds::Button
          @text="Cancel"
          @color="secondary"
          @route={{this.cancelLink.route}}
          @models={{this.cancelLink.models}}
          data-test-edit-form-cancel
        />
      {{/if}}
    </Hds::ButtonSet>
  </div>
</form>